package com.swt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.swt.service.AdminService;
import com.swt.domain.Admin;
import com.swt.mapper.AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
* @author 苏
* @description 针对表【yesapi_shopxo_s_admin(ShopXO商城-管理员)】的数据库操作Service实现
* @createDate 2022-05-16 15:21:33
*/
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;
    @Override
    public List<Admin> selectAlladmin() {
        return adminMapper.selectList(null);
    }

    @Override
    public int delectadmin(String id) {
        return adminMapper.deleteById(id);
    }

    @Override
    public int updateadmin(Admin admin) {
        return adminMapper.updateById(admin);
    }

    @Override
    public int inseradmin(Admin admin) {
        if (!Objects.equals(admin.getUsername(), "")){
            return adminMapper.insert(admin);
        }
        return 0;
    }

    @Override
    public Admin selectbyadmin(String id) {
        return adminMapper.selectById(id);
    }

    @Override
    public Page<Admin> pageadmin(Integer page, Integer start,Admin admin) {
        Page<Admin> userPge = new Page<>(page,start);
        LambdaQueryWrapper<Admin> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(Admin::getCreateTime);
        if (admin != null ){
        lqw.like(StringUtils.isNotEmpty(admin.getUsername()),Admin::getUsername,admin.getUsername());
        lqw.like(StringUtils.isNotEmpty(admin.getMobile()),Admin::getMobile,admin.getMobile());
        lqw.like(StringUtils.isNotEmpty(admin.getRoleId()),Admin::getRoleId,admin.getRoleId());
        lqw.like(StringUtils.isNotEmpty(admin.getGender()),Admin::getGender,admin.getGender());
        }
        return adminMapper.selectPage(userPge,lqw);
    }

    @Override
    public boolean login(Admin admin) {
        LambdaQueryWrapper<Admin> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Admin::getUsername,admin.getUsername())
                .eq(Admin::getLoginPwd,admin.getLoginPwd());
        return adminMapper.selectOne(lqw) != null;
    }
}




